package com.zretc.controller;

import com.zretc.entity.Account;
import com.zretc.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author 何亮龙
 * 前端用户管理控制器
 */
@Controller
@RequestMapping("/front")
public class FrontAccountController {

    private static final Logger logger = LoggerFactory.getLogger(FrontAccountController.class);

    @Autowired
    private AccountService accountService;

    @RequestMapping("/existsOfAccountName")
    @ResponseBody
    public boolean existsOfAccountName(@RequestParam(value = "accountName") String accountName) {
        boolean existsOfAccountName = accountService.existsOfAccountName(accountName);
        return existsOfAccountName;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@RequestParam(value = "accountName") String accountName,
                    @RequestParam(value = "accountGender") String accountGender,
                    @RequestParam(value = "accountPwd") String accountPwd,
                    @RequestParam(value = "accountPhone") String accountPhone,
                    MultipartFile accountPicture
    ) throws IOException {
        logger.info("====注册用户名：" + accountName);
        logger.info("====注册密码：" + accountPwd);
        logger.info("====注册手机号：" + accountPhone);
        // 接收图片
        if (accountPicture.getSize() == 0) {
            /*film.setFilmCover(null);*/
            Account account = new Account(null, accountName, accountGender, accountPwd, accountPhone, null,null);
            accountService.register(account);
        } else {
            // 允许的文件图片类型
            String allowImgType = "image/jpg,image/jpeg,image/png";
            // 允许的文件图片类型集合
            List<String> allowImgTypeList = Arrays.asList(allowImgType.split(","));

            // 判断上传的文件类型是否合法
            String contentType = accountPicture.getContentType(); // 获取上传文件的MIME类型

            // 判断上传的文件MIME类型是否在指定的允许类型集合
            if (allowImgTypeList.contains(contentType) == false) {
                System.out.println("文件类型不匹配!");
                // 重定向注册页面,提示文件不合法
                return "redirect:front/accountRegister.jsp?fileuploaderror=fileuploaderror";
            } else {
                // 1、获取文件名
                String fileAccountCoverName = accountPicture.getOriginalFilename();
                // 自定义文件名 + 文件后缀名
                // 1.1 获取文件后缀名
                String fileAccountCoverSuffix = fileAccountCoverName.substring(fileAccountCoverName.lastIndexOf("."));
                // 1.2 时间戳自定义文件名
                String dataStamp = System.currentTimeMillis() + "";
                // 1.3 存储文件名
                String accountCover = dataStamp + fileAccountCoverSuffix;
                // 2.将文件存储到指定的目录中
                // 将文件存储在指定的服务器中（本地电脑D盘）
                File f = new File("D:/upload/image/" + accountCover);
                try {
                    accountPicture.transferTo(f);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Account account = new Account(null, accountName, accountGender, accountPwd, accountPhone,  accountCover,null); // 数据库中仅需要存储文件名.扩展名
                accountService.register(account);

            }
        }
        // 重定向到前台首页
        return "redirect:accountLogin.jsp";
    }

    @RequestMapping("/login")
    public @ResponseBody
    boolean login(@RequestParam(value = "accountName") String accountName,
                  @RequestParam(value = "accountPwd") String accountPwd,
                  HttpSession session) {

        logger.info("====登录用户名：" + accountName);
        logger.info("====登录密码：" + accountPwd);

        Account account = accountService.login(accountName, accountPwd);
        if (account == null) {
            return false;
        } else {
            // 将用户信息存储session会话中
            session.setAttribute("account", account);
            return true;
        }

    }

    @RequestMapping("/logout")
    public @ResponseBody
    boolean logout(HttpSession session) {

        // 清除session中指定属性
        session.removeAttribute("account");
        return true;
    }

}
